define(['jquery','api','user','util'],function($,api,user,util){
	var vm,
	   channelId,
	   controller = {
	   init:function(id){
	   	   if(!user.checkLogin())return;
		   api.initFooter(3);
		   api.setTitle('绑定银行卡');
		   
		   if(!vm){
		   	 channelId=id;
		     this.render();
		     this.loadPrivinces();
		     this.loadBanks();
		     this.loadCaptcha();
		   }else{
		   	 vm.channelId=id;
		     vm.show=true;
		     if(Object.keys(vm.provinces).length==0)this.loadPrivinces();
		     if(Object.keys(vm.banks).length==0)this.loadBanks();
		   }
	   },
	   render:function(){
	   	   var that=this;
	   	   $('#pages').append('<div id="bind"></div>');
	       require(['text!pages/fund/bind.html','Vue'],function(tpl,Vue){
	            vm=new Vue({
	               el:'#bind',
	               data:{
	               	  show:true,
	               	  channelId:channelId,
	               	  imgSrc:false,
	               	  provinces:{},
	                  citys:{},
	                  banks:{},
	                  form:{
	                 	bankid:'',
	                 	channelid:channelId,
		                bankprovince:'',
		                bankcity:'',
		                bankaccount:'',
		                bankaccountname:'',
		                idcode:'',
		                bankmobile:'',
		                captcha:'',
		                verifycode:'',
		                isprimary:1,
		                upsert:1,
		                bankdeposittype:2
	                 },
	                 captchaImg:''
	               },
	               watch:{
		               'form.bankprovince':function(v){
		                 	this.city='';
		                 	that.initCitys(v);
		                }
		           },
	               methods:{
	                  changeCaptcha:function(){
	                      that.loadCaptcha();
	                  },
	                  getVerify:function(){
						    if(this.form.bankmobile!=''&&this.form.captcha!=''){
						    	util.Verify.send(this.form.bankmobile,this.form.captcha,$('#btnVerify'));
						    }else if(this.form.bankmobile==''){
						        util.msg('请先输入手机号！');
						    }else if(this.form.captcha==''){
						        util.msg('请先输入验证码！');
						    }
		              },
	                  doBind:function(){
		              		var data=this.form;
		                	
		                	if(that.isValid(data)){
		                		data.bankcityname=this.citys[data.bankcity];
		                		data.bankprovincename=this.provinces[data.bankprovince];
		                		data.bankdeposit=this.banks[data.bankid];
		                		api.bind(data,function(rs){
		                		    if(rs.retcode=='0'){
		                		        history.go(-1);
		                		    }else if(rs.retcode){
		                		    	util.dealErrorCode(rs.retcode);
		                		    }
		                		});
		                	}
		              }
	               },
	               template:tpl
	            })
	       })
	   },
	   loadPrivinces:function(){
	       api.privinces(function(rs){
	           if(rs.retcode=='0'){
	               vm.provinces=rs.data;
	           }
	       })
	   },
	   loadBanks:function(){
	       api.banks(function(rs){
	           if(rs.retcode=='0'){
	               vm.banks=rs.data;
	           }
	       })
	   },
	   initCitys:function(v){
	       api.citys(v,function(rs){
	           if(rs.retcode=='0'){
	               vm.citys=rs.data;
	           }
	       })
	   },
	   loadCaptcha:function(){
	       api.captcha(function(rs){
	           if(rs.retcode=='0'){
	               vm.imgSrc='data:image/png;base64,'+rs.imgcode;
	           }
	       })
	   },
	   isValid:function(data){
       	  var error=false,
       	      errorHash={
       	          'verifycode':'请输入验证码！',
       	           bankid:'请选择银行！',
       	           province:'请选择开户省份！',
       	           city:'请选择开户城市！',
       	          'idcode':'请输入正确的身份证号码！',
       	          'bankmobile':'请输入银行预留手机号！',
       	          'bankaccount':'请输入银行卡号！',
       	          'bankaccountname':'请输入持卡人姓名！'
       	      };
       	  for(var key in errorHash){
       	  	  if (data[key]=='') {
		          error=errorHash[key];
		          break;
		      }
       	  }
       	  if(!error&&!util.isIdCardNo(data.idcode)){
       	      error='请输入正确的身份证号码！';
       	  }
		  if(error){
		  	  util.msg(error);
		      return false;
		  }else{
		      return true;
		  }
       },
	   onRouteChange:function(){
	       if(vm)vm.show=false;
	   }
	};
    return controller;
})